"use strict";
function SGoCore()
{
    
    var i;
    this.m_gametree=null;
    this.m_sgfparser=null;
    this.m_board=null;    
    this.m_boardSize=19;
    this.currentChidrenPos=new Array(2*SGFProp.MAX_CHILDREN);

    for (i=0;i<this.currentChidrenPos.length;i++) 
    {       
        this.currentChidrenPos[i]={x:-1,y:-1};
    }    
}

SGoCore.prototype.getDefaultBounds=function()
{
    var b={};
    b.left=b.top=0;
    b.right=b.bottom=this.m_boardSize;
    return b;
}

SGoCore.prototype.getBounds=function()
{
    return this.m_board.m_bounds;
}

SGoCore.prototype.getMargins=function(bounds)
{
    var margins={};
    margins.left   = (bounds.left==0) ? true:false;
    margins.right  = (bounds.right==this.m_boardSize) ? true:false;
    margins.top    = (bounds.top==0) ? true:false;
    margins.bottom = (bounds.bottom==this.m_boardSize) ? true:false;
    return margins;
}

SGoCore.prototype.getChidrenPos=function()
{
    var i,nd,c_nr,node;

    for(i=0;i<this.currentChidrenPos.length;i++)
    {
        this.currentChidrenPos[i].x=-1;
        this.currentChidrenPos[i].y=-1;
    }    
    
    /* get next node stone position*/
    node=this.m_gametree.getNextCurrent();    
    if(node==null)
        return null;
        
    if(node.m_stone!=null)       
    { 
        this.currentChidrenPos[0].x=node.m_stone.m_x;
        this.currentChidrenPos[0].y=node.m_stone.m_y;
    }
    
    /* find current node children position */
    node=this.m_gametree.getCurrent();
    c_nr=node.getNumberOfChilds();
   
    for(i=0;i<c_nr;i++)
    {
        nd=node.getChild(i);
        if(nd.m_stone!=null)
        {
           this.currentChidrenPos[i+1].x=nd.m_stone.m_x;
           this.currentChidrenPos[i+1].y=nd.m_stone.m_y;             
        }           
    }    
    return this.currentChidrenPos;
}

SGoCore.prototype.loadSGF=function(rawSGF)
{
    var sz;

    this.m_sgfparser=new SGoParser();
    this.m_gametree=this.m_sgfparser.decodeSGF(rawSGF);
    this.m_gametree.traverse(this.m_gametree.m_root,0);
	this.m_gametree.goRoot();

    if(this.m_gametree.m_root!=null)
    {

        sz=this.m_gametree.m_root.getProp(SGFProp.ID_SZ);
        if(sz!=null)
            this.m_boardSize=parseInt(sz);
        if(isNaN(this.m_boardSize))
            this.m_boardSize=19;

    }
	this.m_board=new SGoBoard();
    this.m_board.init(this.m_boardSize);
    /* set board bounds */
    this.m_board.setBounds(this.m_gametree.m_bounds,3);
}


SGoCore.prototype.moveNext=function(steps)
{
    var i,j,node,status=false;
    /* -1 go to end */
    if(steps==-1)
        steps=500;
        
    if(this.m_gametree==null)
        return status;
    
    for(i=0;i<steps;i++)
    {
        status=this.m_gametree.moveNext();       
        if(status==true)
        {
            node=this.m_gametree.getCurrent();
            if(node!=null)
            {
                 if(node.m_stone!=null)
                 {
                    this.m_board.makeMove(node,node.m_stone);
                 }
                 this.addProperties(node);                
            }           
        }
        else
            break;
    }
   return status;
}

SGoCore.prototype.movePrev=function(steps)
{
    var i,gt,node,status=false;
    
    if(steps==-1)
        steps=500;

    if(this.m_gametree==null)
        return status;

    for(i=0;i<steps;i++)
    {
         node=this.m_gametree.getCurrent();            
         if((node!=null)&&(node.m_stone!=null))
         {
             this.m_board.takeMove(node,node.m_stone);
         }
         status=this.m_gametree.movePrev();
         if((status==false) ||(node==null))
            break;
    }
    return status;
}

SGoCore.prototype.moveChild=function(cnr)
{
   var node,rv=false;

   if(this.m_gametree!=null)
   {
        rv=this.m_gametree.moveChild(cnr);
        if(rv!=false)
        {
            node=this.m_gametree.getCurrent();
            this.m_board.makeMove(node,node.m_stone);
        }
   }
   return rv;
}

SGoCore.prototype.goRoot=function()
{
    this.m_gametree.goRoot();
}

SGoCore.prototype.addRootProps=function()
{
   if(this.m_gametree!=null)
   {
      this.m_gametree.goRoot();
      this.addProperties(this.m_gametree.getCurrent());
   }

}

SGoCore.prototype.restart=function()
{
   var rv=false;

   if(this.m_gametree!=null)
   {
      this.m_gametree.goRoot();
      this.m_board.clear();
      this.addProperties(this.m_gametree.getCurrent());
      rv=true;
   }
   return rv;
}

SGoCore.prototype.addProperties=function(node)
{
    var p,i;

    if((node!=null)&&(node.m_prop!=null))
    {        
        for(i=0;i<node.m_prop.length;i++)
        {
            p=node.m_prop[i];           
            if((p.m_id==SGFProp.ID_AW) || (p.m_id==SGFProp.ID_AB))
            {
                this.m_board.addMove(p);
            }            
        }
    }

}

SGoCore.prototype.getCurrentComment=function()
{
    var nd;
    nd=this.m_gametree.getCurrent();

	if(nd!=null)
	{
		return nd.m_comment;
	}
	return "";

}

SGoCore.prototype.nextToPlay=function()
{
    var nd, pn;
    nd=this.m_gametree.getCurrent();
    
    if((nd!=null) && (nd.m_stone!=null))
    {
        pn = (nd.m_stone.m_id==SGFProp.ID_B) ? SGFProp.ID_W:SGFProp.ID_B;
    }
    else
    {
        pn = this.m_gametree.m_root.getProp(SGFProp.ID_PL);
        if(pn==null)
            pn=SGFProp.ID_B;
    }
    return pn;
}

SGoCore.prototype.getGameInfo=function(filetype)
{
    var str, str1, node, pn;
    
    str="";

	node=this.m_gametree.m_root;
    if(node==null)
        return;

    if(filetype=="problem")
    {
        pn=node.getProp(SGFProp.ID_PL);
        
        if(pn==null)
        {
            node=node.m_next;
            if((node!=null)&&(node.m_stone!=null))
            {                
                if(node.m_stone.m_color==SGFProp.COLOR_WHITE)
                    pn="W";
                else
                    pn="B";
            }
        }        
        if(pn=="W")
            str="<span style=\"color:#fff\">White to play<span>";
        else
            str="<span style=\"color:#000\">Black to play<span>";

    }
    else
    {
        str+="<span style=\"color:#fff\">";
        str+=node.getPropStr(SGFProp.ID_PW);
        str+="</span>";
        str+=" vs. ";
        str+="<span style=\"color:#000\">";
        str+=node.getPropStr(SGFProp.ID_PB);
        str+="</span> <br/>";        
        str+=node.getPropStr(SGFProp.ID_DT)+", ";
        str+=node.getPropStr(SGFProp.ID_RE)+", ";
        str+=node.getPropStr(SGFProp.ID_EV);            

    }
    return str;
}

SGoCore.prototype.getCurrentNode=function()
{
    var nd;
    nd=this.m_gametree.getCurrent();
    return nd;
}

SGoCore.prototype.getStone=function(i,j)
{
    return this.m_board.m_stones[i][j];    
}

SGoCore.prototype.isHandi=function(i,j)
{
    return this.m_board.isHandi(i,j);    
}

SGoCore.prototype.addMove=function(x,y)
{
    var new_node,stone;
    

    stone=this.getStone(x,y);
    if(stone!=null)
    {
        return false;
    }
    
    new_node=new Node();
    new_node.m_stone=new Prop();
    new_node.m_stone.m_x=x;
    new_node.m_stone.m_y=y;
    new_node.m_stone.m_id=this.nextToPlay();
    new_node.m_stone.m_color=(new_node.m_stone.m_id==SGFProp.ID_B)?SGFProp.COLOR_BLACK:SGFProp.COLOR_WHITE;
    this.m_gametree.addNode(new_node);
    this.m_board.makeMove(new_node, new_node.m_stone);        
    return true;
}




